package com.jketing.entity.ams.work;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.springframework.format.annotation.DateTimeFormat;

import com.jketing.annotation.FieldMeta;
import com.jketing.entity.BaseEntity;

/**
 * MaizeExperiment 玉米试验
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2018-10-11
 * @since   JPA 2.1
 */
@Entity
@Table(name = "MaizeExperiment_")
public class MaizeExperiment extends BaseEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8926592632743352378L;
	/**
	 * name:名称
	 */
	@NotNull
	@FieldMeta(name="String:name",explain="名称")  
	private String name;
	/**
	 * organs:地点
	 */
	@NotNull
	@FieldMeta(name="String:locale",explain="地点")  
	private String locale;
	/**
	 * altitude:海拔
	 */
	@FieldMeta(name="float:altitude",explain="海拔") 
	private Float altitude;    
	/**
	 * varietyNumber:品种数量
	 */
	private Short varietyNumber;    
	/**
	 * repetitionNumber:重复数
	 */
	private Short repetitionNumber; 
	/**
	 * contrast:对照
	 */
	private String contrast;    
	/**
	 * photo:照片
	 */
	private byte[] photo;
	/**
	 * describe:描述
	 */
	@NotNull
	@FieldMeta(name="String:describe",explain="描述") 
	private String describe; 
	/**
	 * sowingTime:播种日期
	 */
	@Past
	@FieldMeta(name="Date:sowingTime",explain="播种日期")	
	private Date sowingTime; 		
	/**
	 * firstFertilizationTtime:第一次施肥
	 */
	@Past
	@FieldMeta(name="Date:firstFertilizationTtime",explain="第一次施肥") 
	private Date firstFertilizationTtime; 	
	/**
	 * secondFertilizationTtime:第二次施肥
	 */
	@Past
	@FieldMeta(name="Date:secondFertilizationTtime",explain="第二次施肥") 
	private Date secondFertilizationTtime;
	/**
	 * harvestDate:收获日期
	 */
	@Past
	@FieldMeta(name="Date:harvestDate",explain="收获日期") 
	private Date harvestDate;
	/**
	 * varieties:品种集合
	 */
	@FieldMeta(name="Entity:varieties",explain="品种集合")
	protected Set<Variety> varieties = new HashSet<Variety>();
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="f_locale")
	public String getLocale() {
		return locale;
	}
	public void setLocale(String locale) {
		this.locale = locale;
	}
	
	public Float getAltitude() {
		return altitude;
	}
	public void setAltitude(Float altitude) {
		this.altitude = altitude;
	}
	
	public Short getVarietyNumber() {
		return varietyNumber;
	}
	public void setVarietyNumber(Short varietyNumber) {
		this.varietyNumber = varietyNumber;
	}
	
	public Short getRepetitionNumber() {
		return repetitionNumber;
	}
	public void setRepetitionNumber(Short repetitionNumber) {
		this.repetitionNumber = repetitionNumber;
	}
	
	public String getContrast() {
		return contrast;
	}
	public void setContrast(String contrast) {
		this.contrast = contrast;
	}
	
	@Lob
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	
	@Column(name="f_describe")
	public String getDescribe() {
		return describe;
	}
	public void setDescribe(String describe) {
		this.describe = describe;
	}
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Temporal(value = TemporalType.DATE)
	public Date getSowingTime() {
		return sowingTime;
	}
	public void setSowingTime(Date sowingTime) {
		this.sowingTime = sowingTime;
	}
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Temporal(value = TemporalType.DATE)
	public Date getFirstFertilizationTtime() {
		return firstFertilizationTtime;
	}
	public void setFirstFertilizationTtime(Date firstFertilizationTtime) {
		this.firstFertilizationTtime = firstFertilizationTtime;
	}
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Temporal(value = TemporalType.DATE)
	public Date getSecondFertilizationTtime() {
		return secondFertilizationTtime;
	}
	public void setSecondFertilizationTtime(Date secondFertilizationTtime) {
		this.secondFertilizationTtime = secondFertilizationTtime;
	}
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Temporal(value = TemporalType.DATE)
	public Date getHarvestDate() {
		return harvestDate;
	}
	public void setHarvestDate(Date harvestDate) {
		this.harvestDate = harvestDate;
	}
	
	@OneToMany(mappedBy = "productionTest", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Variety> getVarieties() {
		return varieties;
	}
	public void setVarieties(Set<Variety> varieties) {
		this.varieties = varieties;
	}
	
	/**
	 * 增加品种
	 * 
	 * @param entity
	 */
	public void add(Variety entity) {
		if (!varieties.contains(entity)) {
			entity.setProductionTest(this);
			varieties.add(entity);
		}
	}	
	
	/**
	 * 删除品种
	 * 
	 * @param entity
	 */
	public void remove(Variety entity) {
		if (varieties.contains(entity)) {
			entity.setProductionTest(null);
			varieties.remove(entity);
		}
	}
	
	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof MaizeExperiment)) {
			return false;
		}
		MaizeExperiment other = (MaizeExperiment) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
}
